package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.condition.RequestConditionHolder;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author L5775
 */
public class UserContext {

    public static final String USER_IN_SESSION = "USER_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION = "EXPRESSION_IN_SESSION";
    /**
     * 通过springmvc提供的工具类,让我们在任意地方直接获取到session,request,response
     * @return
     */
    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }

    //把当前登录用户存放到session中
    public static void setCurrentUser(Employee employee){
        getSession().setAttribute(USER_IN_SESSION,employee);
    }

    //从session中获取当前登录用户
    public static Employee getCurrentUser(){
        return (Employee) getSession().getAttribute(USER_IN_SESSION);
    }

    //把权限表达式集合存放到session中
    public static void setExpressions(List<String> expressions){
        getSession().setAttribute(EXPRESSION_IN_SESSION,expressions);
    }

    //从session中获取权限表达式集合
    public static List<String> getExpressions(){
        return (List<String>) getSession().getAttribute(EXPRESSION_IN_SESSION);
    }
}
